package com.example.util;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import cc.vv.lkbasecomponent.util.LKScreenUtil;

/**
 * Android状态栏工具类 (1.改变状态栏颜色；2.让图片嵌入状态栏中)
 */
public class StatusBarColorUtil {


    private static View statusView;

    /**
     * 修改状态栏颜色
     *
     * @param activity    需要修改状态栏颜色的界面
     * @param statusColor 修改的颜色
     */
    public static void changeStatusBarByColor(Activity activity, int statusColor) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);  // 设置状态栏透明
            View statusView = createStatusView(activity, statusColor);
            ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView(); // 添加 statusView 到布局中
            decorView.addView(statusView);
            // 设置根布局的参数
            ViewGroup rootView = (ViewGroup) ((ViewGroup) activity.findViewById(android.R.id.content)).getChildAt(0);
            rootView.setFitsSystemWindows(true);  //必须设置，否则布局嵌入进状态栏之中
            rootView.setClipToPadding(true);
        }
    }

    /**
     * 生成一个和状态栏大小相同的矩形条
     *
     * @return 状态栏矩形条
     */
    private static View createStatusView(Activity activity, int color) {
        // 获得状态栏高度
        int resourceId = activity.getResources().getIdentifier("status_bar_height", "dimen", "android");
        int statusBarHeight = activity.getResources().getDimensionPixelSize(resourceId);
        // 绘制一个和状态栏一样高的矩形
        statusView = new View(activity);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, statusBarHeight);
        statusView.setLayoutParams(params);
        statusView.setBackgroundColor(color);
        return statusView;
    }
public static void hideStatus(){
    if (statusView!=null){
        statusView.setVisibility(View.GONE);
    }


}
    public static void showStatus(){
        if (statusView!=null){
            statusView.setVisibility(View.VISIBLE);
        }


    }
    /**
     * 图片作背景时，状态栏透明
     *
     * @param activity
     */
    public static void changeStatusBarByImage(Activity activity, final boolean is) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);   //透明状态栏
            // 设置根布局的参数
            ViewGroup rootView = (ViewGroup) ((ViewGroup) activity.findViewById(android.R.id.content)).getChildAt(0);
            rootView.setFitsSystemWindows(is);
            rootView.setClipToPadding(true);
        }
    }

    /**
     * 图片作背景时，状态栏透明
     *
     * @param activity
     */
    public static void changeStatusBarByImageHome(final Activity activity, final boolean is) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);   //透明状态栏
//          activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);//透明导航栏
            // 设置根布局的参数
            ViewGroup rootView = (ViewGroup) ((ViewGroup) activity.findViewById(android.R.id.content)).getChildAt(0);
            rootView.setFitsSystemWindows(is);
            rootView.setClipToPadding(true);
        }

    }
    public static void setHideVirtualKey(Window window){
        //保持布局状态
        int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE|
                //布局位于状态栏下方
                View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION|
//                //全屏
//                View.SYSTEM_UI_FLAG_FULLSCREEN|
                //隐藏导航栏
//                View.SYSTEM_UI_FLAG_HIDE_NAVIGATION|
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
        if (Build.VERSION.SDK_INT>=19){
            uiOptions |= 0x00001000;
        }else{
            uiOptions |= View.SYSTEM_UI_FLAG_LOW_PROFILE;
        }
        window.getDecorView().setSystemUiVisibility(uiOptions);
    }
    /**
     * 获取手机状态栏高度
     *
     * @param context
     * @return 返回状态栏高度的像素值
     */
    public static int getStatusBarHeight(Context context) {
        int result = 0;
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelSize(resourceId);
        } else {
            //说明:如果回去手机状态栏高度失败，默认赋值18dp
            result = LKScreenUtil.dp2px(18);
        }
        return result;
    }

    public static void setPaddingTopToStatusBar(View view) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            int statusBarHeight = getStatusBarHeight(view.getContext());
            view.setPadding(0, statusBarHeight, 0, 0);
        }
    }

  public static void setHideNavigion(final Activity activity){
          //保持布局状态
          int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE|
                  //布局位于状态栏下方
                  View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION|
//                //全屏
//              View.SYSTEM_UI_FLAG_FULLSCREEN|
                  //隐藏导航栏
//              View.SYSTEM_UI_FLAG_HIDE_NAVIGATION|
                  View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
          if (Build.VERSION.SDK_INT>=19){
              uiOptions |= 0x00001000;
          }else{
              uiOptions |= View.SYSTEM_UI_FLAG_LOW_PROFILE;
          }
          activity.getWindow().getDecorView().setSystemUiVisibility(uiOptions);
          activity.getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
              @Override
              public void onSystemUiVisibilityChange(int visibility) {
                  setHideVirtualKey( activity.getWindow());
              }
          });



  }



}
